home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6417 < prev    next >
Encoding:
Text File  |  1996-08-05  |  10.6 KB  |  318 lines

  1. Path: Belgium.EU.net!box!pahint
  2. From: pahint@eunet.be (Pieter Hintjens)
  3. Newsgroups: comp.lang.c
  4. Subject: Universal include file
  5. Date: 24 Feb 1996 17:55:22 GMT
  6. Organization: EUnet Belgium, Leuven, Belgium
  7. Message-ID: <4gnjea$30m@news.Belgium.EU.net>
  8. NNTP-Posting-Host: box.eunet.be
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I am working on a project to build a 'Universal Include File'.
  12. The point is to encapsulate the peculiarities of every system
  13. under the sun into one include file.  Maybe this is not doable,
  14. but I already have something halfway decent.
  15.  
  16. The UIF should include all the standard header files: certainly
  17. all those defined in the ANSI C standard, plus those that are
  18. common enough to be valuable.  (E.g. socket interface files.)
  19.  
  20. The objective is to build more portable C programs without the
  21. usual mess of #ifdef's.
  22.  
  23. This is the file that I've been using for a few years now... I
  24. hope it shows the general direction.  Forgive a few idiosyncratic
  25. macros. ;-)
  26.  
  27. Any comments and specific information gratefully received!
  28.  
  29. Cheers,
  30. Pieter A. Hintjens
  31.  
  32.  
  33. /*===========================================================================*
  34.  *                                                                           *
  35.  *  prelude.h   Universal header file for C programming                      *
  36.  *                                                                           *
  37.  *  Written:    93/03/29  Pieter Hintjens                                    *
  38.  *  Revised:    96/02/22                                                     *
  39.  *                                                                           *
  40.  *  This header encapsulates all generally-useful include files and defines  *
  41.  *  various useful things.  To use, specify as first include file in code.   *
  42.  *  Under UNIX, you should use the 'c' compile script; this defines the      *
  43.  *  _UNIX_TYPE symbol, needed to get system-specific #include's correct.     *
  44.  *                                                                           *
  45.  *  Author:     Pieter A. Hintjens                                           *
  46.  *              Pijlstraat 9                                                 *
  47.  *              2060 Antwerpen, Belgium                                      *
  48.  *              ph@mymail.com                                                *
  49.  *              (+323) 231.5277                                              *
  50.  *                                                                           *
  51.  *  Copyright (c) 1991-96 Pieter A. Hintjens.  May be freely distributed.    *
  52.  *===========================================================================*/
  53.  
  54. #ifndef _PRELUDE_INCLUDED               /*  Allow multiple inclusions        */
  55. #define _PRELUDE_INCLUDED
  56.  
  57.  
  58. /*- Generally-useful include files ------------------------------------------*/
  59.  
  60. #include <ctype.h>
  61. #include <limits.h>
  62. #include <stdarg.h>
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #include <time.h>
  67. #include <signal.h>
  68. #include <errno.h>
  69. #include <fcntl.h>
  70.  
  71.  
  72. /*- Establish the compiler and computer system ------------------------------*/
  73. /*
  74.  *  Defines one or more of these symbols, for use in any non-portable
  75.  *  code:
  76.  *
  77.  *  __WINDOWS__         Microsoft C/C++ with Windows calls
  78.  *  __MSDOS__           File system is MS-DOS
  79.  *  __VMS__             System is VAX/VMS or Alpha/OpenVMS
  80.  *  __UNIX__            System is UNIX
  81.  *  __MAC__             System is Mac/OS
  82.  *
  83.  *  __32BIT__           OS/compiler is 32 bits
  84.  *  __64BIT__           OS/compiler is 64 bits (only Digital UNIX so far)
  85.  */
  86.  
  87. #define __32BIT__                       /*  Assume 32-bit OS/compiler        */
  88.  
  89. #if (defined (WIN32) || defined (WINDOWS))
  90. #   undef __WINDOWS__
  91. #   define __WINDOWS__
  92. #   undef __MSDOS__
  93. #   define __MSDOS__
  94. #endif
  95.  
  96. /*  MSDOS      is Microsoft C                                                */
  97. /*  _MSC_VER   is Microsoft C                                                */
  98. /*  __TURBOC__ is Borland Turbo C                                            */
  99. #if (defined (MSDOS) || defined (_MSC_VER) || defined (__TURBOC__))
  100. #   undef __MSDOS__
  101. #   define __MSDOS__
  102. #endif
  103.  
  104. /*  VMS        is VAX C (VAX/VMS)                                            */
  105. /*  __VMS      is Dec C (Alpha/OpenVMS)                                      */
  106. /*  __vax__    is gcc                                                        */
  107. #if (defined (VMS) || defined (__VMS) || defined (__vax__))
  108. #   undef __VMS__
  109. #   define __VMS__
  110. #endif
  111.  
  112. /*  unix          is SunOS                                                   */
  113. /*  __unix__      is gcc                                                     */
  114. /*  _POSIX_SOURCE is various UNIX systems, maybe also VAX/VMS                */
  115. #if (defined (unix) || defined (__unix__) || defined (_POSIX_SOURCE))
  116. #   if !defined (__VMS__)
  117. #       undef __UNIX__
  118. #       define __UNIX__
  119. #       if (defined (__alpha))          /*  Digital UNIX is 64-bit           */
  120. #           undef  __32BIT__
  121. #           define __64BIT__
  122. #       endif
  123. #   endif
  124. #endif
  125.  
  126. /*  Untested                                                                 */
  127. #if (defined (_MAC))
  128. #   undef __MAC__
  129. #   define __MAC__
  130. #endif
  131.  
  132.  
  133. /*- System-specific include files -------------------------------------------*/
  134.  
  135. #if (defined (__TURBOC__))              /*  Borland Turbo-C uses this        */
  136. #   include <alloc.h>                   /*    name for its include file      */
  137. #else
  138. #   include <malloc.h>
  139. #endif
  140.  
  141. #if (defined (__WINDOWS__))             /*  When __WINDOWS__ is defined,     */
  142. #   include <windows.h>                 /*    so is __MSDOS__                */
  143. #endif
  144.  
  145. #if (defined (__MSDOS__))
  146. #   include <dos.h>
  147. #   include <io.h>
  148. #   include <fcntl.h>
  149. #   include <sys\stat.h>
  150. #   include <sys\types.h>
  151. #endif
  152.  
  153. #if (defined (__UNIX__))
  154. #   include <netdb.h>
  155. #   include <unistd.h>
  156. #   include <netinet/in.h>
  157. #   include <sys/param.h>
  158. #   include <sys/socket.h>
  159. #   include <sys/time.h>
  160. #   include <sys/stat.h>
  161. #   include <sys/types.h>
  162. /*  Specific includes; the c script defines one of:
  163.  *      _UNIX_AIX        _UNIX_APOLLO     _UNIX_A_UX
  164.  *      _UNIX_BS_DOS     _UNIX_HP_UX      _UNIX_IRIX
  165.  *      _UNIX_LINUX      _UNIX_NCR        _UNIX_NETBSD
  166.  *      _UNIX_NEXT       _UNIX_OSF1       _UNIX_SCO
  167.  *      _UNIX_PYRAMID    _UNIX_SUNOS      _UNIX_ULTRIX
  168.  *      _UNIX_GENERIC
  169.  */
  170. #   if defined (_UNIX_AIX)              /*  Specific includes for IBM/AIX    */
  171. #       include <sys/select.h>
  172. #   endif
  173. #endif
  174.  
  175. #if (defined (__VMS__))
  176. #   include <netdb.h>
  177. #   include <unixio.h>
  178. #   include <in.h>
  179. #   include <param.h>
  180. #   include <socket.h>
  181. #   include <time.h>
  182. #   include <stat.h>
  183. #   include <types.h>
  184. #endif
  185.  
  186.  
  187. /*- Data types --------------------------------------------------------------*/
  188.  
  189. typedef unsigned char  byte;
  190. typedef unsigned short word;
  191. typedef int            Bool;
  192. #if (defined (__32BIT__))
  193. typedef unsigned long  dword;           /*  Long is 32 bits                  */
  194. typedef long int       lint;
  195. #else
  196. typedef unsigned int   dword;           /*  Long is 64 bits, int is 32 bits  */
  197. typedef int            lint;
  198. #endif
  199. typedef void (*function) (void);        /*  Address of simple function       */
  200. #define local static void               /*  Shorthand for local functions    */
  201.  
  202.  
  203. /*- Check compiler data type sizes ------------------------------------------*/
  204.  
  205. #if (UCHAR_MAX != 0xFF)
  206. #   error "Cannot compile: must change definition of 'byte'."
  207. #endif
  208. #if (USHRT_MAX != 0xFFFFU)
  209. #   error "Cannot compile: must change definition of 'word'."
  210. #endif
  211. #if (defined (__32BIT__))
  212. #   if (ULONG_MAX != 0xFFFFFFFFUL)
  213. #       error "Cannot compile: must change definition of 'dword'."
  214. #   endif
  215. #else
  216. #   if (UINT_MAX != 0xFFFFFFFFU)
  217. #       error "Cannot compile: must change definition of 'dword'."
  218. #   endif
  219. #endif
  220.  
  221.  
  222. /*- Pseudo-functions --------------------------------------------------------*/
  223.  
  224. #define ever            (;;)                /*  for ever { ... }             */
  225. #define until(expr)     while (!(expr))     /*  do { ... } until (expr)      */
  226. #define error(expr)     ((expr) < 0)        /*  if error (expr) { ...        */
  227. #define loop(var,n)     for (var = 0; var < n; var++)
  228.  
  229. #define strlast(str)    (str [strlen (str) - 1])
  230. #define strterm(str)    (str [strlen (str))
  231. #define streq(s1, s2)   (!strcmp ((s1), (s2)))
  232. #define strneq(s1, s2)  (strcmp ((s1), (s2)))
  233. #define strused(s)      (*(s) != 0)
  234. #define strnull(s)      (*(s) == 0)
  235. #define strclr(s)       (*(s) = 0)
  236.  
  237. #define bit_msk(bit)    (1 << (bit))
  238. #define bit_set(x, bit) ((x) |=  bit_msk (bit))
  239. #define bit_clr(x, bit) ((x) &= ~bit_msk (bit))
  240. #define bit_tst(x, bit) ((x) &   bit_msk (bit))
  241.  
  242. #define tblsize(x)      (sizeof (x) / sizeof ((x) [0]))
  243. #define tbllast(x)      (x [tblsize (x) - 1]);
  244.  
  245. #if (defined (random))
  246. #   undef random
  247. #   undef randomize
  248. #endif
  249. #if (defined (min))
  250. #   undef min
  251. #   undef max
  252. #endif
  253.  
  254. #if (defined (__32BIT__))
  255. #define random(num)     (int) ((long) rand () % (num))
  256. #else
  257. #define random(num)     (int) ((int)  rand () % (num))
  258. #endif
  259. #define randomize()     srand ((unsigned) time (NULL))
  260. #define min(a,b)        (((a) < (b))? (a): (b))
  261. #define max(a,b)        (((a) > (b))? (a): (b))
  262.  
  263.  
  264. /*- ASSERT and debugging ----------------------------------------------------*/
  265.  
  266. #if (defined (DEBUG))
  267.     /*  Define _Assert function here locally                                 */
  268.     local _Assert (char *File, unsigned Line)
  269.     {
  270.         fflush  (stdout);
  271.         fprintf (stderr, "\nAssertion failed: %s, line %u\n", File, Line);
  272.         fflush  (stderr);
  273.         abort   ();
  274.     }
  275. #   define ASSERT(f)            \
  276.         if (f)                  \
  277.             ;                   \
  278.         else                    \
  279.             _Assert (__FILE__, __LINE__)
  280. #else
  281. #   define ASSERT(f)
  282. #endif
  283.  
  284.  
  285. /*- Boolean operators and constants -----------------------------------------*/
  286.  
  287. #if (defined (TRUE))
  288. #   undef TRUE
  289. #   undef FALSE
  290. #endif
  291. #define TRUE           (1==1)
  292. #define FALSE          (1==0)
  293. #define AND            &&
  294. #define OR             ||
  295.  
  296.  
  297. /*- Symbolic constants ------------------------------------------------------*/
  298.  
  299. #define FORK_ERROR      -1              /*  Return codes from fork()         */
  300. #define FORK_CHILD      0
  301. #if (!defined (LINE_MAX))
  302. #   define LINE_MAX     255
  303. #endif
  304. #define TAB '\t'
  305.  
  306.  
  307. /*- System-specific definitions ---------------------------------------------*/
  308.  
  309. #if (defined (__VMS__))
  310. #   define RET_OKAY             1       /*  Return codes for main()          */
  311. #   define RET_ERROR            0       /*  VMS does it its own way.         */
  312. #else
  313. #   define RET_OKAY             0       /*  On non-VMS systems 0 is okay,    */
  314. #   define RET_ERROR            1       /*    and 1 is an error.             */
  315. #endif
  316.  
  317. #endif                                  /*  Include PRELUDE.H                */
  318.